package reports;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import model.entity.Item;
import model.entity.Product;
import model.entity.container.ProductGroup;
import model.entity.container.StorageUnit;
import model.management.ProductGroupManager;
import model.management.StorageUnitManager;
import reports.builders.IBuilder;
import reports.display.ReportTable;
import reports.display.ReportTableRow;
import reports.visitor.IVisitor;

public class ExpiredItemReport extends Report
{

  private static final String[] COLUMNS      = new String[]
                                             {
      "Description	",
      "Storage Unit	",
      "Product Group	",
      "Entry Date		",
      "Expire Date	",
      "Item Barcode	"
                                             };

  private static final String   TITLE        = "Expired Items Report";
  private IVisitor              visitor;

  private List<Item>            expiredItems = new ArrayList<Item>();
  private ReportTable           table;

  public ExpiredItemReport()
  {
    super(TITLE);
    table = new ReportTable(null, COLUMNS);
    visitor = new ExpiredItemReportVisitor();
  }

  @Override
  protected void buildReport(IBuilder<?> builder)
  {
    builder.addTitle("Expired Items").startTable(table.getColumnCount())
        .addTableHeader(table.getColumnTitles());

    Iterator<ReportTableRow> itr = table.getRows();
    while (itr.hasNext())
    {
      builder.addRow(itr.next().getCells());
    }

    builder.endTable();
    //
    // builder.addTableHeader(Arrays.asList("Description", "Storage Unit", "Product Group",
    // "Entry Date", "Expire Date", "Item Barcode").iterator());
    // builder.addRow(Arrays.asList("Toothpaste", "Bathroom", "Toiletries", "7/4/2011",
    // "10/12/2015",
    // "4000000000001").iterator());
    // builder.addRow(Arrays.asList("Brown sugar", "Pantry", "Baking", "8/4/2012", "9/4/2013",
    // "4000000000002").iterator());
    //
  }

  @Override
  protected void visitData() throws Exception
  {
    // this should be where you iterate through the container tree and gather all the data you
    // need for the report.
    StorageUnitManager.getInstance().preOrderAccept(visitor);
    ProductGroupManager.getInstance().preOrderAccept(visitor);
  }

  @Override
  protected void interpretData() throws Exception
  {

    for (Item item : expiredItems)
    {
      ReportTableRow row = table.createRow();
      row.setCell(0, item.getProduct().getDescription());
      row.setCell(1, item.getContainer().getStorageUnit().getName());
      if (item.getProduct().getContainers().size() == 1)
      {
        row.setCell(2, "");
      }
      else
      {
        row.setCell(2, item.getContainer().getName());
      }
      row.setCell(3, item.getEntryDate());
      row.setCell(4, item.getExpirationDate());
      row.setCell(5, item.getBarcode().getCode());
      table.addRow(row);
    }
  }

  private class ExpiredItemReportVisitor implements IVisitor
  {
    @Override
    public void visit(ProductGroup productgroup)
    {
      Iterator<Item> allItems = productgroup.getItems();

      while (allItems.hasNext())
      {
        Item item = allItems.next();
        if (item.isExpired())
        {
          expiredItems.add(item);
        }
      }
    }

    @Override
    public void visit(StorageUnit storageunit)
    {
      Iterator<Item> allItems = storageunit.getItems();

      while (allItems.hasNext())
      {
        Item item = allItems.next();
        if (item.isExpired())
        {
          expiredItems.add(item);
        }
      }
    }

    @Override
    public void visit(Item item)
    {
      // do nothing
    }

    @Override
    public void visit(Product product)
    {
      // do nothing
    }
  }
}
